﻿@{
    ViewBag.Title = "Invoice";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model List<e.Store.Model.Entities.GetInvoiceDetailsResult>

<div class="col-lg-12">

    <div id="productData" class="panel panel-danger">
        <div class="panel-heading">
            <h3 class="panel-title"><span id="title">New Invoice</span> <span class="pull-right"><a href="/Billing/Sales" title="View All Sales" class="btn-sm btn-primary">
                <span class="btn-label"><i class="icon-table"></i></span>View All Sales </a>
            </span></h3>
        </div>
        <div class="panel-body">

            <div id="hiddenElements">


                @Html.Hidden("InvoiceOID", (string)ViewBag.InvoiceOID, new { id = "InvoiceOID" })

            </div>

            <div class="col-lg-4 col-lg-offset-4">
                <div class="form-group">

                    <label class="control-label" for="txtProductCode">Product Code</label>
                    <input class="form-control" id="txtProductCode" />

                </div>
                <div class="form-group">
                    <div class="pull-right col-lg-offset-2">
                        <button id="btnSearch" class="btn btn-labeled btn-success">
                            <span class="btn-label"><i class="icon-search icon-white"></i></span>Search</button>
                    </div>
                </div>

            </div>

            <div class="well col-lg-10 col-lg-offset-1">
                <div class="row">

                    <div class="col-xs-6 col-sm-6 col-md-6">
                        <address>
                            <strong>Customer:</strong>
                            <span id="customerName">@ViewBag.CustomerName</span>
                            <br>
                            @*<abbr title="User">User:</abbr>*@
                            <strong>User:</strong>
                            <span id="userName">@ViewBag.UserName</span>
                        </address>
                    </div>

                    <div class="col-xs-6 col-sm-6 col-md-6 text-right">
                        <p>
                            <strong>Date:</strong>
                            <span id="todayDate">@ViewBag.TodayDate</span>
                        </p>
                        <p>
                            <strong>Invoice #:</strong>
                            <span id="invoiceNo">@ViewBag.InvoiceNo</span>
                        </p>
                    </div>
                </div>
                <div class="row">
                    <div class="text-center">
                        <h3>Invoice Details</h3>
                    </div>

                    <div class="col-sm-12 col-md-10 col-md-offset-1 table-responsive">

                        <table id="tblInvoiceDetails" class="table table-hover table-responsive">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th class="text-center">Quantity</th>
                                    <th class="text-center">Price(Rs.)</th>
                                    <th class="text-center">Discount(%)</th>
                                    <th class="text-center">Tax(%)</th>
                                    <th class="text-center">Total(Rs.)</th>
                                    <th class="text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model)
                                {
                                    if (item.ProductOID > 0)
                                    {
                                    <tr>
                                        <td class="col-lg-4">@item.ProductName / @item.Description</td>
                                        <td class="col-lg-2 text-center">
                                            @item.Quantity</td>
                                        <td class="col-lg-2 text-center">@item.PricePerPiece</td>
                                        <td class="col-lg-2 text-center">@item.ProductDiscount</td>
                                        <td class="col-lg-2 text-center">@item.ProductTax</td>
                                        <td class="col-lg-2 text-center">@item.ProductTotalPrice</td>
                                        <td class="col-lg-2 text-center">
                                            <button type="button" class="btn btn-sm btn-danger" title="Remove">
                                                <span class="icon icon-remove"></span>
                                            </button>
                                        </td>
                                    </tr>
                                    }
                                }

                            </tbody>
                        </table>

                        <table id="tblBillDetails" class="table table-hover table-responsive">

                            <tbody>
                                <tr>
                                    <td ></td>
                                    <td class="col-lg-6"></td>
                                   
                                    <td>
                                        <h4>Subtotal</h4>
                                    </td>
                                    <td class="text-right" colspan="2">
                                        <h5 id="subTotalAmount"><strong>Rs @Model.Sum(c => c.ProductTotalPrice)</strong></h5>
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td></td>
                                   
                                    <td>
                                        <h4>Total Tax</h4>
                                    </td>
                                    <td class="text-right" colspan="2">
                                        <h5 id="taxAmount"><strong>Rs @Model.Sum(c => c.ProductTax)</strong></h5>
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td></td>
                                   
                                    <td>
                                        <h4>Total Discount</h4>
                                    </td>
                                    <td class="text-right" colspan="2">
                                        <h5 id="discountAmount"><strong>Rs @Model.Sum(c => c.ProductDiscount)</strong></h5>
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td></td>
                                   
                                    <td style="color: red">
                                        <h3>Total</h3>
                                    </td>
                                    <td class="text-right" style="color: red" colspan="2">
                                        <h3 id="totalAmount"><strong>Rs  @Model.Sum(c => c.ProductTotalPrice)</strong></h3>
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>
                                        <button type="button" id="btnSaveContinue" class="btn btn-default" title="Save this invoice and make new invoice">
                                            <span class="icon icon-pause"></span>&nbsp;Save and Continue
                                        </button>
                                    </td>
                                    <td>
                                        <button type="button" id="btnPayNow" class="btn btn-success" title="Close the invoice and proceed">
                                            Pay Now <span class="icon icon-play"></span>
                                        </button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {

        $("#txtProductCode").focus();

        $("#btnSearch").click(function () {

            // alert($("#txtProductCode").val());
            if ($("#txtProductCode").val() == "") {
                create("themeroller", { title: "Error!", text: "Please enter product code." }, {
                    custom: true,
                    expires: false
                });
                $("#txtProductCode").focus();

                return false;
            }

            $.ajax({
                type: "POST",
                async: true,
                url: '/Billing/AddItemToInvoice',
                data: {
                    productCode: $("#txtProductCode").val(),
                    invoiceOID: $("#InvoiceOID").val()
                },
                dataType: "json",
                processdata: true,
                success: function (result) {

                    if (result.Result == false) {
                        create("themeroller", { title: 'Error!', text: result.Message }, {
                            custom: true,
                            expires: false
                        });
                    } else {
                        // redirect it to page or reload data in table
                        window.location.href = "/Billing/MakeInvoice";
                    }
                },
                error: function (e) {

                }
            });

            $("#txtProductCode").val("");
            $("#txtProductCode").focus();

        });

    });// ready
</script>
